package com.cc.bill.controller;

import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.cc.bill.entity.CardEntity;
import com.cc.bill.service.CardService;
import com.cc.common.utils.R;
import com.cc.common.utils.ResponseData;
import com.cc.user.entity.UserEntity;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpSession;
import java.text.ParseException;
import java.util.Arrays;
import java.util.List;
import java.util.Map;


/**
 * 
 *
 * @author leifengyang
 * @email leifengyang@gmail.com
 * @date 2020-12-11 14:54:41
 */
@RestController
@RequestMapping("card/card")
@Api(tags = "打卡")
public class CardController {
    @Autowired
    private CardService cardService;

    /**
     * 信息
     */
    @GetMapping("/info/{cid}")
    public R info(@PathVariable("cid") Long cid){
		CardEntity card = cardService.getById(cid);
        return R.ok().put("card", card);
    }

    /**
     * 保存
     * 只需要传入  打卡地址，打卡类型（0：外出，1：正常），外出原因，用户uid，备注
     */
    @PostMapping("/save")
    public R save(@RequestBody CardEntity card){
		cardService.save(card);
        return R.ok();
    }

    /**
     * 修改
     */
    @PostMapping("/update")
    public R update(@RequestBody CardEntity card){
		cardService.updateById(card);
        return R.ok();
    }

    /**
     * 删除
     */
    @PostMapping("/delete")
    public R delete(@RequestBody Long[] cids){
		cardService.removeByIds(Arrays.asList(cids));
        return R.ok();
    }

    /**
     * 添加打卡记录
     * 只需要传入  打卡地址，打卡类型（0：外出，1：正常），外出原因，用户uid，备注，图片的imageUrls集合
     */
    @ApiOperation("打卡")
    @PostMapping("/addCard")
    public ResponseData<CardEntity> addCard(@ApiParam(value = "打卡地址",required = true) @RequestParam("cardAddress") String cardAddress,
                                           @ApiParam(value = "打卡类型(0:外出，1:正常)",required = true) @RequestParam("cardType") Integer cardType,
                                           @ApiParam("外出原因") @RequestParam(value = "outworkComments",required = false) String outworkComments,
                                           @ApiParam("备注") @RequestParam(value = "workRemarks",required = false) String workRemarks,
                                           @ApiParam("外出打卡上传的图片") @RequestBody(required = false) List<String> imageUrls, HttpSession session){
        UserEntity user = (UserEntity) session.getAttribute("loginuser");
        Long uid = user.getUid();

        CardEntity cardEntity = cardService.addCard(uid,cardAddress,cardType,outworkComments,workRemarks,imageUrls);
        return ResponseData.success(cardEntity);
    }

    @ApiOperation("通过用户id查看考勤记录")
    @GetMapping("/getKQ")
    public ResponseData<List<CardEntity>> getKQ(@ApiParam("起始日期") @RequestParam(value = "begin",required = false)
                           String begin,
                   @ApiParam("终止日期") @RequestParam(value = "end",required = false)
                               String end, HttpSession session) throws ParseException {
        UserEntity user = (UserEntity) session.getAttribute("loginuser");
        Long uid = user.getUid();

        List<CardEntity> cardEntities = cardService.getKQ(uid,begin,end);
        return ResponseData.success(cardEntities);
    }

    @ApiOperation("后台查询所有考勤记录")
    @GetMapping("/getAllKQ")
    public R getAllKQ(@RequestParam Map<String,Object> params) throws ParseException {
        Page<CardEntity> cardEntities = cardService.getAllKQ(params);
        return R.ok().put("page",cardEntities);
    }

}
